package com.aloha.springframework.context.support;

import com.aloha.springframework.beans.BeansException;
import com.aloha.springframework.beans.factory.support.DefaultListableBeanFactory;
import com.aloha.springframework.beans.factory.ConfigurableListableBeanFactory;

/**
 * @author DaiZhiHeng
 * @description 为刷新容器提供关键步骤代码的实现(创建BeanFactory并注册BeanDefinition ， 不过注册的具体实现交给子类去实现)
 * @date 2023/7/3 9:07
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {

    /**
     * 组合一个BeanFactory对象
     */
    private DefaultListableBeanFactory beanFactory;

    // 模板方法：交给子类去加载BeanDefinition
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);


    @Override
    protected void refreshBeanFactory() throws BeansException {
        // 1.创建工厂
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        this.beanFactory = beanFactory;

        // 2.加载BeanDefinition
        loadBeanDefinitions(beanFactory);
    }

    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
